package com.lkdont.android.media.opengl

import android.opengl.GLES20
import android.opengl.GLException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer

/**
 * OpenGL工具
 *
 * @author lqd
 */
internal class Utils {

    companion object {
        /**
         * 检查OpenGL发生的错误，如果发生错误，直接抛出错误
         *
         * @param msg 信息
         */
        @Throws(GLException::class)
        @JvmStatic
        fun checkGLError(msg: String) {
            val error = GLES20.glGetError()
            if (error != GLES20.GL_NO_ERROR) {
                throw GLException(error, msg + ": glError 0x" + Integer.toHexString(error))
            }
        }

        const val SIZEOF_FLOAT = 4

        /**
         * Allocates a direct float buffer, and populates it with the float array data.
         */
        @JvmStatic
        fun createFloatBuffer(coords: FloatArray): FloatBuffer {
            // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it.
            val bb = ByteBuffer.allocateDirect(coords.size * SIZEOF_FLOAT)
            bb.order(ByteOrder.nativeOrder())
            val fb = bb.asFloatBuffer()
            fb.put(coords)
            fb.position(0)
            return fb
        }
    }
}